#!/usr/bin/env python
# -*- coding: utf-8 -*- 

#Kiwi Bot
#Copyright (C) 2009 Mathieu "MathX" Xhonneux
#Modified by @nge1nCorsic4 in 2010

#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License along
#with this program; if not, write to the Free Software Foundation, Inc.,
#51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import kiwi, kiwi.config

class Plugin:
	def __init__(self, main):
		self.main = main
		self.log = self.main.log
		self.clients = self.main.clients
		self.output = Output(self.main.rcon)
		self.eventHandlers = []
	def registerEvent(self, eventType, handler):
		self.main.registerHandler(eventType, handler)
		self.eventHandlers.append(handler)
	def start(self):
		self.onStart()
	def stop(self):
		for evt, list in self.main.handlers.iteritems():
			for handler in list:
				if handler in self.eventHandlers:
					self.main.handlers[evt].remove(handler)
					self.eventHandlers.remove(handler)
		self.onStop()
class Output:
	def __init__(self, rcon):
		self.rcon = rcon
		self.prefix = '%s ^4>'%(kiwi.config.configuration().kiwi.name)
	def say(self, msg):
		responses = []
		msg = self.prefix+' ^3'+msg
		for i in self.getList(msg):
			m = 'say ^3%s' %(i)
			responses.append(self.rcon.send(m))
		return responses
	def tell(self, id, msg):
		responses = []
		msg = self.prefix+' ^3'+msg
		for i in self.getList(msg):
			m = 'tell %s ^3%s' %(id, i)
			responses.append(self.rcon.send(m))
		return responses
	def bigtext(self, msg):
		msg = 'bigtext "%s"'%(msg)
		return self.rcon.send(msg)
	def send(self, msg):
		return self.rcon.send(msg)
	def getList(self, string):
		max = 125
		if len(string) > max :
			list = string.split(' ')
			string = []
			s = ''
			for a in list:
				if a == list[-1] :
					s += a+' '
					string.append(s)
				elif len(s)+len(a) < max:
					s += a+' '
				else:
					string.append(s)
					s = a+' '
			return string
		else:
			return [string]
	